/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-04-28
 * Time: 19:22
 */

class Node{
    public int val;
    public Node left;
    public Node right;

    public Node(int val){
        this.val = val;
    }
}
public class BinarySearchTree {
    public Node root = null;

    public Node search(int key){//先懂得如何查找 再去考虑插入
        Node cur = root;
        while (cur != null){
            if (cur.val < key){
                cur = cur.right;
            }else if (cur.val > key){
                cur = cur.left;
            }else {
                return cur;
            }
        }
        return null;
    }


    public boolean insert(int key){
        if (root == null){
            root = new Node(key);
            return true;
        }
        Node cur = root;
        Node parent = null;
        while (cur != null){
            if (cur.val < key){
                parent = cur;
                cur = cur.right;
            }else if (cur.val > key){
                parent = cur;
                cur = cur.left;
            }else {
                return false; // 不能有相同的数据进行插入
            }
        }
        Node node = new Node(key);
        if (parent.val < key){
            parent.right = node;
        }else if (parent.val > key){
            parent.left = node;
        }
        return false;
    }

    public void remove(int key){
        Node cur = root;
        Node parent = null;
        while (cur != null){
            if (cur.val == key){
                removeNode(cur,parent);
                break;
            }else if (cur.val < key){
                parent = cur;
                cur = cur.right;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
    }

    public void removeNode(Node cur,Node parent){
        if (cur.left == null){ // cur的左边为空
            if (cur == root){
                root = cur.right;
            }else if (cur == parent.left){
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if (cur.right == null){
            if (cur == root){
                root = cur.left;
            }else if (cur == parent.left){
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else{
            Node targetParent = cur;
            Node target = cur.right;
            while (target.left != null){
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if (target == targetParent.left){
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }

        }

    }

    public void inOrder(Node root){
        if (root == null){
            return;
        }
        System.out.print(root.val+" ");
        inOrder(root.left);
        inOrder(root.right);
    }




    public static void main(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        int[] array = {5,3,7,1,4,6,8,0,2,9};
        for (int i = 0; i < array.length; i++) {
            binarySearchTree.insert(array[i]);
        }
        System.out.println(binarySearchTree.search(0).val);
        binarySearchTree.inOrder(binarySearchTree.root);
        System.out.println();
        binarySearchTree.remove(6);
        binarySearchTree.inOrder(binarySearchTree.root);
    }


}
